package com.ssm.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ssm.entity.User;
import com.ssm.service.UserServiceImpl;
import com.ssm.util.LogHelper;

import jakarta.servlet.http.HttpSession;

@Controller
@RequestMapping("user")
public class UserController {
	@Autowired
	private UserServiceImpl userService;
	
	@RequestMapping("/show")
	public String show(){		
		return "user :: content";
	}
	
	@RequestMapping("/list")
	@ResponseBody
	public String index(){
		return userService.findAll();
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public String add(User user){
        return userService.add(user);
	}
	//实现Restful 风格的URL
	@GetMapping("/get/{uid}")
	@ResponseBody
	public String get(@PathVariable int uid){
        return userService.get(uid);
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public String update(User user){
        return userService.update(user);
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public String delete(int uid){
        return userService.delete(uid);
	}
	@RequestMapping("/profile")
	public String profile(HttpSession session, Model model){
		// 检查用户是否登录
		User loginUser = (User) session.getAttribute("loginUser");

		//   如果用户未登录，则重定向到登录页面
		if (loginUser == null) {
			LogHelper.warn("访问个人中心页面时用户未登录");
			return "redirect:/login";
		}
		
		// 确保model中包含loginUser
		model.addAttribute("loginUser", loginUser);
		LogHelper.info("用户 " + loginUser.getUname() + " 访问个人中心页面");
		return "profile :: content";
	}
	
	@RequestMapping("/userInfo")
	public String userInfo(HttpSession session, Model model){
		// 检查用户是否登录
		User loginUser = (User) session.getAttribute("loginUser");
		if (loginUser == null) {
			LogHelper.warn("访问用户信息页面时用户未登录");
			return "redirect:/login";
		}
		
		// 确保model中包含loginUser
		model.addAttribute("loginUser", loginUser);
		LogHelper.info("用户 " + loginUser.getUname() + " 访问用户信息页面");
		return "userinfo :: content";
	}
	
	@RequestMapping("/updateProfile")
	@ResponseBody
	public Map<String, Object> updateProfile(User user, HttpSession session){
		Map<String, Object> result = new HashMap<>();
		
		try {
			String updateResult = userService.update(user);
			if (updateResult.contains("\"Status\":\"ok\"")) {
				// 更新session中的用户信息
				User loginUser = (User) session.getAttribute("loginUser");
				if (loginUser != null) {
					loginUser.setAlias(user.getAlias());
					loginUser.setPasswd(user.getPasswd());
					loginUser.setLogo(user.getLogo());
					session.setAttribute("loginUser", loginUser);
				}
				
				result.put("Status", "ok");
				result.put("msg", "个人信息更新成功");
				LogHelper.info("用户 " + user.getUname() + " 更新个人信息成功");
			} else {
				result.put("Status", "error");
				result.put("msg", "更新失败，请稍后再试");
				LogHelper.error("用户 " + user.getUname() + " 更新个人信息失败");
			}
		} catch (Exception e) {
			result.put("Status", "error");
			result.put("msg", "系统错误：" + e.getMessage());
			LogHelper.error("更新个人信息时发生异常: " + e.getMessage());
		}
		
		return result;
	}
}
